import {getUrlParam,filterData } from '@/static/js/filter.js';
export default{
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				dialog: false,
				searchContent: '',
				current: '',
				nextDate_start_date:'',
				start_date:currentDate,
				userName: '',
				start: currentDate,
			    end_date: currentDate,
			    nextDate:'',
			    other:'请输入',
				followWay: '',
				pickerArray: [],  //定义默认的下拉框数组
				pickerIndex: 0,// 下拉框索引
				pickerType:'',  //下拉框传递的类名
				pickerShow:false,								
				moreList:[], //事项list
				tz:'',
				bmi: '',
				symptomList:[],
				objList:[],
				healthInformation:{},
				btnStatus: true, //状态按钮
				status: '', //从上个页面带过来的状态
				id: '',
				evaluate: '', //随访评价
				data:{	
					symptom:[], //症状list
					physicalSign:[{name:'体重（kg）',value:''},{name:'BMI',value:''},{name:'血压（mmHg）',value:'',value1:'',value2:''},{name:'血糖（mmol/L）',value:''}], //体征list
				}
			}
		},
		onLoad() {
			this.memberList();
			// 获取健康师内容
			this.$api.findByHealthId().then((res)=>{
				this.healthInformation = res.data.Result;
			});	
			
			if(getUrlParam('status')) {
				this.status = getUrlParam('status');
			} 
			

		},
		mounted() {
			// 获取id  编辑
			this.id = getUrlParam('id');
			if(this.id) {
				uni.setNavigationBarTitle({
					title:"修改随访记录"
				})
				this.$api.followDetail(this.id).then((res) => {
					this.data = res.data.Result;
					this.start_date = res.data.Result.floowDate;
					this.data.drink = res.data.Result.appLifeGuidanceEntity.drink;
					this.data.smoke = res.data.Result.appLifeGuidanceEntity.smoke;
					this.data.sport = res.data.Result.appLifeGuidanceEntity.sport;
					this.data.saltIntake = res.data.Result.appLifeGuidanceEntity.saltUptake;
					this.data.psychology = res.data.Result.appLifeGuidanceEntity.psychology;
					this.data.follow = res.data.Result.appLifeGuidanceEntity.complianceBehavior;
					uni.createSelectorQuery().select('#otherEditor').context((a) => {this.editorCtx = a.context; this.editorCtx.setContents({html: res.data.Result.appLifeGuidanceEntity.other})}).exec();
					this.data.nextDate = res.data.Result.nextDate;
					this.physicalSign = res.data.Result.appLifeGuidanceEntity;
					this.data.physicalSign[2].value1 = res.data.Result.physicalSign[2].value.split(',')[0]
					this.data.physicalSign[2].value2 = res.data.Result.physicalSign[2].value.split(',')[1]
					this.symptomList = res.data.Result.symptom; //症状
					this.moreList = res.data.Result.moreMethood; //更多事项
					this.nextDate = res.data.Result.nextDate; //下次随访日期
					this.userName = res.data.Result.memberNmae; //会员
					this.userId = res.data.Result.userId;
					this.data.motion = res.data.Result.sport == '不运动'  ? '不运动' : JSON.parse(res.data.Result.sport).motion;  //运动
					this.data.duration = res.data.Result.sport != '不运动' && JSON.parse(res.data.Result.sport).duration || '';  //运动
					this.evaluate = res.data.Result.evaluate;
				})
			}
		},
		watch:{
			$route(v) {
				console.log(v)
			}
		},
		methods:{
			// 富文本输入框 
			editorInput(e){
				this.data.default_other = e.detail.html;
				// this.$set(this,this.editType,e.detail.html)
			},
			// 打开picker
			showModel(e){
				this.pickerType = e;
				this.pickerShow = true
				this.pickerArray = e=='followUpMethod'?['在线沟通','上门服务','其他方式']:
								   e=='smoke'?['不吸烟',1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,'20以上']:
								   e=='drink' ? ['不喝酒',1,2,3,4,5,6,7,8,9,10,'10以上']: 
								   e== 'motion' ? ['不运动',1,2,3,4,5,6,7,8,9,10,'10以上']: 
								   e== 'duration' ? [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
													81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,'120以上']: 
								   e=='saltIntake'?['正常','偏少','偏多']:
								   e=='psychology'?['无','优','良','中','差']:
								   e=='follow'?['无','优','良','中','差']:[]
								   
				this.$refs.picker.$el.click()
			},
			// 添加事项
			addmore(){
				this.moreList.push({})
			},
			// 添加体征
			addsign(){
				this.data.physicalSign.push({})
			},
			// 添加症状
			addsymptomType(){				
				this.symptomList.push({})
			},
			//保存按钮
			seveAdd(statusType){
				if(!this.userId){
					uni.showToast({title:'请选择会员',icon:'none'})
					return;
				}
				// if(!this.data.followUpMethod) {
				// 	uni.showToast({title:'请选择随访方式',icon:'none'})
				// 	return;
				// }
				// if(!this.data.physicalSign[0].value) {
				// 	uni.showToast({title:'请输入体重',icon:'none'})
				// 	return;
				// } else if(!this.data.physicalSign[1].value) {
				// 	uni.showToast({title:'请输入BMI',icon:'none'})
				// 	return;
				// } else if(!this.data.physicalSign[2].value) {
				// 	uni.showToast({title:'请输入血压值',icon:'none'})
				// 	return;
				// }else if(!this.data.physicalSign[3].value) {
				// 	uni.showToast({title:'请输入血糖（mmol/L）',icon:'none'})
				// 	return;
				// }
				
				// if(!this.data.smoke) {
				// 	uni.showToast({title:'请选择吸烟情况',icon:'none'})
				// 	return;
				// } else if(!this.data.drink) {
				// 	uni.showToast({title:'请选择喝酒情况',icon:'none'})
				// 	return;
				// } else if(!this.data.motion ) {
				// 	uni.showToast({title:'请选择运动情况',icon:'none'})
				// 	return; 
				// } else if(!this.data.saltIntake) {
				// 	uni.showToast({title:'请选择摄盐情况',icon:'none'})
				// 	return; 
				// }else if(!this.data.psychology) {
				// 	uni.showToast({title:'请选择心理调整情况',icon:'none'})
				// 	return; 
				// }else if(!this.data.follow) {
				// 	uni.showToast({title:'请选择遵从行为',icon:'none'})
				// 	return; 
				// }
				
				// if(!this.nextDate) {
				// 	uni.showToast({title:'请选择下次随访日期',icon:'none'})
				// 	return;
				// }
				// 生活指导
				const appLifeGuidanceEntity = {
					complianceBehavior:this.data.follow,
					drink:this.data.drink,
					psychology:this.data.psychology,
					saltUptake:this.data.saltIntake,
					smoke:this.data.smoke,
					sport:this.data.motion == '不运动'?this.data.motion : JSON.stringify({motion:this.data.motion,duration:this.data.duration}),
					other:this.data.default_other,
					id: this.id ? Number(this.id) : '',
				}
				if(!appLifeGuidanceEntity.id) {
					delete appLifeGuidanceEntity.id;
				}	
				const moreMethood = this.moreList && this.moreList.map((v,i)=>{
					return {
						name:i,
						value:v.value
					}
				})
				const symptom = this.symptomList && this.symptomList.map((v,i)=>{
					return {
						name:i,
						value:v.value
					}
				})
				this.data.physicalSign[2].value = !this.data.physicalSign[2].value1 && !this.data.physicalSign[2].value1 ? '' : (this.data.physicalSign[2].value1 || '') +','+ (this.data.physicalSign[2].value2 || '')
				// delete this.data.physicalSign[2].value1
				// delete this.data.physicalSign[2].value2
				const data = {
					appLifeGuidanceEntity:appLifeGuidanceEntity,
					evaluate:'未评价',
					floowDate:this.start_date,
					followUpMember:this.userId,
					followUpMethod:this.data.followUpMethod,
					followUpPersonnel:this.healthInformation.id,
					moreMethood:moreMethood,
					nextDate:this.nextDate,
					physicalSign:this.data.physicalSign,
					status:statusType,
					symptom:symptom,
					userId:this.userId,
					id: this.id ? Number(this.id) : '',
				}
				if(!data.id) {
					delete data.id;
				}		
				// 判断点击的是签名发送的话  弹出弹出框
				if(statusType == 1) {
					const _this = this;
					uni.showModal({
						title: '提示',
						content: '请确认数据准确，发送后不可修改。',
						success: function(res) {
							if (res.confirm) {
								_this.$api[_this.id ? 'modifyFollow' : 'createFollow'](data).then((ares)=>{
									if(ares.statusCode == 200) {
										_this.btnStatus = false;
										uni.navigateTo({
											url:'./followList'
										})
									}
								});
							} else if (res.cancel) {
								// console.log('用户点击取消');
							}
						}
					});
				} else {
					this.$api[this.id ? 'modifyFollow' : 'createFollow'](data).then((res)=>{
						if(res.statusCode == 200) {
							uni.showToast({
								title: '保存成功'
							})
							uni.navigateBack({
								delta: 1
							})
						}
					});
				}
				
			},

			// 删除症状
			delSymptomType(index) {
				this.symptomList.splice(index,1);
			},
			
			// 删除增加的体征项目
			delPhysicalSign(index){
				this.data.physicalSign.splice(index,1);
			},
			
			// 删除事项
			delMoreList(index) {
				this.moreList.splice(index,1)
			},
			
			// 本次随访日期
			bindDateChange(e) {  //日期选择
				this.start_date = e.target.value;
				this.nextDate_start_date = filterData(new Date(new Date(e.target.value).getTime()+86400000)).substr(0,10);
				console.log(this.nextDate_start_date)
			},
			
			// 下次随访日期
			bindDateChange2(e) {		
				this.nextDate =  e.target.value;
			},
			
			pickerChange(e) { 
				const _this = this;
				_this.$set(_this.data,_this.pickerType,_this.pickerArray[e.detail.value])
				// console.log(_this.data)
			},
			radioChange(e) {
				this.list && this.list.map((res) => {
					if (res.userId == e.detail.value) {
						this.userName = res.realName;
						this.userId = res.userId;
					}
				})
			},
			// 获取会员列表数据
			memberList() {
				this.$api.selectMembershipList({
					str: this.searchContent
				}).then((res) => {
					if (res.data.StatusCode == 200) {
						this.list = res.data.Result && res.data.Result.list.map((e) => {
							e.userId = e.userId + '';
							return e
						});
					}
				})
			},
			 // 获取当前时间
		    getDate(type) {
			   const date = new Date();
			   let year = date.getFullYear();
			   let month = date.getMonth() + 1;
			   let day = date.getDate();
			  
			   if (type === 'start') {
				year = year - 60;
			   } else if (type === 'end') {
				year = year + 2;
			   }
			   month = month > 9 ? month : '0' + month;;
			   day = day > 9 ? day : '0' + day;
			   return `${year}-${month}-${day}`;
		    },
		}
	}